package com.travel.dao.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.travel.base.impl.BaseDaoImpl;
import com.travel.bean.Ticket;
import com.travel.dao.TicketDao;
@Repository("ticketDaoImpl")
public class TicketDaoIml extends BaseDaoImpl<Ticket> implements TicketDao {

	
	/**
	 * 按照分页展示票
	 */
	@Override
	public List<Ticket> queryByPage(Map map, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		Session session=this.hibernateTemplate.getSessionFactory().openSession();
		Criteria criteria=session.createCriteria(Ticket.class);
		if(map!=null){
			criteria.add(Restrictions.allEq(map));
		}
		
		if(pageNum!=null&&pageSize!=null){
			criteria.setFirstResult((pageNum-1)*pageSize);
			criteria.setMaxResults(pageSize);
		}
		List<Ticket> tickets=criteria.list();
		if(session!=null&&session.isOpen()){
			session.close();
		}
		return tickets;
	}

	@Override
	public Integer queryByPageCount(Map map) {
		// TODO Auto-generated method stub
		Session session=this.hibernateTemplate.getSessionFactory().openSession();
		Criteria criteria=session.createCriteria(Ticket.class);
		if(map!=null){
			criteria.add(Restrictions.allEq(map));
		}
		criteria.setProjection(Projections.rowCount());
		Integer totalCount=((Long)criteria.uniqueResult()).intValue();
		if(session!=null&&session.isOpen()){
			session.close();
		}
		return totalCount;
	}

	/**
	 * 按照路线id查票
	 */
	@Override
	public Ticket getTicketByRouteId(Integer routeId) {
		// TODO Auto-generated method stub
		Session session=this.hibernateTemplate.getSessionFactory().openSession();
		Criteria criteria=session.createCriteria(Ticket.class);
		criteria.add(Restrictions.eq("routeid", routeId));
		Ticket ticket=(Ticket) criteria.uniqueResult();
		if(session!=null&&session.isOpen()){
			session.close();
		}
		return ticket;
		
	}

	

}
